<template>
  <el-card>
    <div slot="header">
      <el-form
        :inline="true"
        ref="ruleForm"
        :model="ruleForm"
        size="small"
        class="demo-form-inline"
      >
        <el-form-item label="工序编码" prop="route_code">
          <el-input
            v-model="ruleForm.route_code"
            placeholder="请输入工单编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="工序名称" prop="route_name">
          <el-input
            v-model="ruleForm.route_name"
            placeholder="请输入工单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="enable_flag">
          <el-select v-model="ruleForm.enable_flag" placeholder="请选择">
            <el-option value="">全部</el-option>
            <el-option value="是">已启用</el-option>
            <el-option value="否">未启用</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >查询</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          plain
          @click="add"
          >新增</el-button
        >
        <el-button size="small" type="danger" icon="el-icon-delete" plain
          >删除</el-button
        >
        <el-button
          style="float: right"
          icon="el-icon-refresh"
          size="small"
          @click="Refresh"
          circle
        ></el-button>
      </div>
    </div>

    <div>
      <el-table :data="tableData" style="width: 100%" max-height="600" v-loading="isloading">
        <el-table-column
          fixed
          prop="route_code"
          label="工艺路线编号"
          width="auto"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="productionbtn(scope.row.route_id)"
              >{{ scope.row.route_code }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="route_name"
          label="工艺路线名称"
          width="auto"
        >
        </el-table-column>
<el-table-column
          align="center"
          prop="route_desc"
          label="工艺路线说明"
          width="auto"
        >
        </el-table-column>
        <el-table-column
          prop="enable_flag"
          label="是否启用"
          width="auto"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.enable_flag === '是' ? 'primary' : 'info'"
              disable-transitions
            >
              <div>{{ scope.row.enable_flag }}</div>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="remark" label="备注" width="auto">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="auto" align="center">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row.route_id)" type="text" size="small"
              >修改</el-button
            >
            <el-button @click="del(scope.row.route_id)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :pageindex="PageIndex"
        :pagesize="PageSize"
        :total="total"
        @CurrentChange="CurrentChange"
        @SizeChange="SizeChange"
      /> 
    </div> 


  <el-dialog
    class="box-dialog"
    :title="title"
    :visible.sync="dialogFormVisible"
    @close="closeDialog"
    :close-on-click-modal="false"
    custom-class="ss"
  >
    <TechnologyInput
      :ruleForms="ruleForms" @saves="saves" @statuss="statuss" @processChange="processChange" :ProcessList="ProcessList" :dialogFormVisibles="dialogFormVisibles" :titles="titles" :isloading="isloading" :ruleFormss="ruleFormss" :ruless="ruless" @update="update" @adds="adds" :rules="rules" @save="save" @status="status" :isupdate="isupdate" :tableDatas="tableDatas"
    ></TechnologyInput>
  </el-dialog>
  </el-card>
</template>

<script>
import Pagination from "../../components/Pagination.vue";
import TechnologyInput from "../../components/TechnologyInput.vue";

export default {
  components: {Pagination,TechnologyInput},
  data() {
    return {
        isupdate:false,
        dialogFormVisible:false,
        dialogFormVisibles:false,
        title:"",
        titles:"",
        PageIndex: 1,
      PageSize: 10,
      total: 0,
        isloading:false,
      ruleForm: {
        route_code: "",
        route_name: "",
        enable_flag: "",
      },
      ruleForms: {
        route_id: "",
        route_code: "",
        route_name: "",
        route_desc: "",
        enable_flag: "",
        remark: "",
        create_by: "",
        create_time: "",
        update_by: "",
        update_time: "",
      },
      ruleFormss: {
        record_id:"",
        route_id:"",
        process_id:"",
        process_code:"",
        process_name:"",
        order_num:0,
        link_type:"",
        default_pre_time:0,
        default_suf_time:0,
        key_flag:"",
        remark:"",
        create_by:"",
        create_time:"",
        update_by:"",
        update_time:"",
      },
      rules: {
        route_code: [
          { required: true, message: "请输入工艺路线编号", trigger: "blur" },
        ],
        route_name: [
          { required: true, message: "请输入工艺路线名称", trigger: "blur" },
        ],
        enable_flag: [
          { required: true, message: "请选择是否启用", trigger: "blur" },
        ],
      },
      ruless: {
        order_num: [
          { required: true, message: "请输入工艺路线编号", trigger: "blur" },
        ],
        process_id: [
          { required: true, message: "请选择工艺路线名称", trigger: "blur" },
        ],
        key_flag: [
          { required: true, message: "请选择是否关键", trigger: "blur" },
        ],
        link_type: [
          { required: true, message: "请选择下一道工序关系", trigger: "blur" },
        ]
      },
      tableData: [],
      tableDatas: [],
      tableDatass: [],
      ProcessList:[]
    };
  },
  methods: {
    processChange(e){
      let data=this.ProcessList.filter(x=>x.process_id===e)[0];
      this.ruleFormss.process_code=data.process_code;
      this.ruleFormss.process_name=data.process_name;
    },
    adds(e){
      this.dialogFormVisibles=true;
      this.titles="新增工艺组成";
      this.getprocess();
    },
    update(e){
      const loading = this.$loading();
      this.$http.putForm('/api/Technology',{pro:this.ruleForms})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dialogFormVisible=false;
                                this.databind();
                            }
                        }).catch(err=>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                loading.close();
                        })
    },
    statuss(e){
      this.dialogFormVisibles=false;
    },
    saves(e){
      this.ruleFormss.route_id=this.ruleForms.route_id;
      const loading = this.$loading();
      this.$http.postForm('/api/Technology/AddPro_Routes_Process',{pro:this.ruleFormss})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dialogFormVisibles=false;
                                this.databinds(this.ruleFormss.route_id);
                            }
                        }).catch(err=>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                 loading.close();
                        })
    },
    status(e){
      this.dialogFormVisible=false;
    },
    save(e){
      const loading = this.$loading();
      this.$http.postForm('/api/Technology/AddPro_Routes',{pro:this.ruleForms})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dialogFormVisible=false;
                                this.databind();
                            }
                        }).catch(err=>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                                loading.close();
                        })
    },
    productionbtn(){

    },
    add(){
this.dialogFormVisible = true;
this.isupdate = false;
      this.title = "新增工序";
    //   this.islook=false;
    //   this.isupdate=false;
      this.ruleForms={};
    //   this.tableDatas=[];
    },
    Refresh(){
        this.databind();
    },
    handleEdit(e) {
      this.dialogFormVisible = true;
      this.isupdate=true;
    //   this.islook=true;
      this.title = "修改工艺路线";
    this.databinds(e);
    },
    del(e) {
    //   if(e != undefined){
    //             this.$confirm('此操作将永久删除数据, 是否继续?','提示',{
    //                 confirmButtonText:'确定',
    //                 cancelButtonText:'取消',
    //                 type:'warning'
    //             })
    //             .then(()=>{
    //                 const loading = this.$loading();
    //                 this.$http.delete('/api/ProcessDesign',{params:{Id:e}})
    //                     .then(res =>{
    //                         if(res.data.code == 200){
    //                             this.ShowMsg(res.data.msg,'success');
    //                             this.databind();
    //                         }
    //                         else{
    //                             this.ShowMsg(res.data.msg,'warning');
    //                         }
    //                     }).catch(err =>{
    //                         this.ShowMsg('服务器繁忙，请稍后重试','error');
    //                     }).finally(()=>{
    //                             loading.close();
    //                     })
    //             }).catch(()=>{})
    //         }
    },
    closeDialog(){

    },
    CurrentChange(e) {
      this.PageIndex = e;
      this.databind();
    },
    SizeChange(e) {
      this.PageSize = e;

      //从第一页开始
      this.PageIndex = 1;
      this.databind();
    },
    getprocess(){
this.$http
        .request({
          type: "get",
          url: "/api/Technology/GetALLPro_Processes"
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            this.ProcessList=res.data.data;
          }
        })
        .catch((err) => {
          this.ShowMsg('服务器繁忙，请稍后重试','error');
        })
        .finally(() => {
          
        });
    },
    databinds(e){
this.isloading=true;
      this.$http
        .request({
          type: "get",
          url: "/api/Technology/GetPro_Route_Processes",
          params: {
            Id:e
          },
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            this.ruleForms=res.data.data.data;
            this.tableDatas = res.data.data.list;
          }
        })
        .catch((err) => {
          this.ShowMsg('服务器繁忙，请稍后重试','error');
        })
        .finally(() => {
          this.isloading=false;
        });
    },
    databind(){
this.isloading=true;
      this.$http
        .request({
          type: "get",
          url: "/api/Technology",
          params: {
            PageIndex: this.PageIndex,
            PageSize: this.PageSize,
            route_code: this.ruleForm.route_code,
            route_name: this.ruleForm.route_name,
            status: this.ruleForm.enable_flag,
          },
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.total = res.data.count;
          }
        })
        .catch((err) => {
          this.ShowMsg('服务器繁忙，请稍后重试','error');
        })
        .finally(() => {
          this.isloading=false;
        });
    },
    submitForm(formName) {
      this.databind();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.databind();
    },
  },
  created(){
    this.databind();
  }
};
</script>

<style>
.ss {
  min-width: 800px;
}
</style>